package com.quan.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quan.commons.core.biz.support.MyBaseServiceImpl;
import com.quan.commons.core.utils.PageUtils;
import com.quan.mall.entity.MallDeliveryAddress;
import com.quan.mall.entity.UmsMember;
import com.quan.mall.entity.UmsMemberLevel;
import com.quan.mall.service.UmsMemberService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.quan.mall.commons.vo.UmsMemberReceiveAddressVo;
import com.quan.mall.entity.UmsMemberReceiveAddress;
import com.quan.mall.mapper.UmsMemberReceiveAddressMapper;
import com.quan.mall.service.UmsMemberReceiveAddressService;


@Slf4j
@Service
public class UmsMemberReceiveAddressServiceImpl extends MyBaseServiceImpl<UmsMemberReceiveAddressMapper, UmsMemberReceiveAddress> implements UmsMemberReceiveAddressService {

    @Autowired
	private UmsMemberReceiveAddressMapper mapper;

    @Autowired
    private UmsMemberService umsMemberService;

	@Override
    public List<UmsMemberReceiveAddressVo> queryList(PageUtils pageUtils, UmsMemberReceiveAddressVo vo) {
        QueryWrapper<UmsMemberReceiveAddress> queryWrapper = new QueryWrapper<UmsMemberReceiveAddress>(vo);

        List<UmsMemberReceiveAddress> list = super.list(pageUtils, queryWrapper);
        List<UmsMemberReceiveAddressVo> collect = list.stream().map(item -> {
            UmsMemberReceiveAddressVo bean = new UmsMemberReceiveAddressVo();
            BeanUtils.copyProperties(item, bean);
            return bean;
        }).collect(Collectors.toList());

        return collect;
    }

    @Override
    public PageUtils queryPage(PageUtils pageUtils, UmsMemberReceiveAddressVo vo) {
        QueryWrapper<UmsMemberReceiveAddress> queryWrapper = new QueryWrapper<UmsMemberReceiveAddress>(vo);

        if(StringUtils.isNotBlank(vo.getKeyword())) {
            queryWrapper.and(w -> {
                w.like("contact", vo.getKeyword()).or().like("phone", vo.getKeyword());
            });
        }

        final IPage<UmsMemberReceiveAddress> page = super.selectPage(pageUtils, queryWrapper);

        PageUtils ps = new PageUtils(page);

        final List<UmsMemberReceiveAddress> records = page.getRecords();

        final List<UmsMemberReceiveAddress> collect = records.stream().map((item) -> {
            UmsMemberReceiveAddressVo bean = new UmsMemberReceiveAddressVo();
            BeanUtils.copyProperties(item, bean);

            final UmsMember member = this.umsMemberService.getById(item.getMemberId());
            if(null != member) {
                bean.setNickname(member.getNickname());
            }

            return bean;
        }).collect(Collectors.toList());

        ps.setRecords(collect);
        return ps;
    }

    @Override
    public UmsMemberReceiveAddress getDefaultAddress() {
        final UmsMemberReceiveAddress def = super.getOne(new QueryWrapper<UmsMemberReceiveAddress>().eq("is_default", 1), false);
        return def;
    }

    @Override
    public boolean save(UmsMemberReceiveAddress entity) {
        if (entity.getIsDefault()) { // 默认地址：0->否；1->是
            UpdateWrapper<UmsMemberReceiveAddress> updateWrapper = new UpdateWrapper<UmsMemberReceiveAddress>();
            updateWrapper.set("is_default", false);
            updateWrapper.eq("member_id", entity.getMemberId());
            super.update(updateWrapper);
        }

        return super.save(entity);
    }

    @Override
    public boolean updateById(UmsMemberReceiveAddress entity) {
        if (entity.getIsDefault()) { // 默认地址：0->否；1->是
            UpdateWrapper<UmsMemberReceiveAddress> updateWrapper = new UpdateWrapper<UmsMemberReceiveAddress>();
            updateWrapper.set("is_default", false);
            updateWrapper.eq("member_id", entity.getMemberId());
            super.update(updateWrapper);
        }

        return super.updateById(entity);
    }

    @Override
    public void changeDefault(UmsMemberReceiveAddress entity) {
        UpdateWrapper<UmsMemberReceiveAddress> updateWrapper = new UpdateWrapper<UmsMemberReceiveAddress>();
        updateWrapper.set("is_default", false);
        updateWrapper.eq("member_id", entity.getMemberId());
        super.update(updateWrapper);

        UpdateWrapper<UmsMemberReceiveAddress> updateWrapperCur = new UpdateWrapper<UmsMemberReceiveAddress>();
        updateWrapperCur.set("is_default", true);
        updateWrapperCur.eq("id", entity.getId());
        updateWrapperCur.eq("member_id", entity.getMemberId());
        super.update(updateWrapperCur);

    }

}